<?php

namespace App\Listeners\OpenShop;

use App\Enums\OpenShop\InvestmentPromotion\MainProcessEnum;
use App\Events\OpenShop\JoinSucceededSmsSentEvent;
use App\Events\OpenShop\OpenShopProcessEvent;
use App\Listeners\BaseListener;
use App\Models\OpenShop\InvestmentPromotion\OpenShopMainProcessModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopProcessDetailModel;
use App\Service\OpenShop\Import\HistoryOrder\HistoryOrderParam;
use Illuminate\Support\Facades\Log;

class OpenShopProcessListener extends BaseListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Handle the event.
     *
     * @param OpenShopProcessEvent $event
     * @return void
     */
    public function handle(OpenShopProcessEvent $event)
    {
        Log::channel('listener')
           ->info(__CLASS__ . " handle ProcessNoticeListener...mainProcessId: {$event->mainProcessId} in...");

        // 获取主订单信息
        $info = OpenShopMainProcessModel::query()->where('id', $event->mainProcessId)->first();

        if ($info->childLocationOrder == 1 && $event->mainProcessType == MainProcessEnum::MAIN_PROCESS_INVESTMENT_SIGN) {
            return;
        }

        $processDescSuffix = '';
        if (HistoryOrderParam::$importOrderMode == true) {
            $processDescSuffix = " （跳流程）";
        }

        OpenShopProcessDetailModel::query()->insert([
            'mainProcessId'   => $event->mainProcessId,
            'mainProcessType' => $event->mainProcessType,
            'mainProcessDesc' => MainProcessEnum::MAIN_PROCESS_MAP[$event->mainProcessType],
            'processType'     => $event->processType,
            'processDesc'     => MainProcessEnum::PROCESS_TREE_MAP[$event->mainProcessType][$event->processType] . $processDescSuffix,
            'status'          => MainProcessEnum::SUCCESS,
            "createdAt"       => time(),
            "updatedAt"       => time(),
            "createdByUid"    => $event->uid,
            "updatedByUid"    => $event->uid
        ]);
    }

}
